﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;

public partial class Admin_NewShipment : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindParcelType();
            bindCountry();
           // bindoginedMemberInfo();
        }
    }

    private void bindCountry()
    {
        BLLShipment bll = new BLLShipment();
        ddlCountry.DataSource = bll.getCountries();
        ddlCountry.DataTextField = "Country";
        ddlCountry.DataValueField = "ID";
        ddlCountry.DataBind();

        ddlReCountry.DataSource = ddlCountry.DataSource;
        ddlReCountry.DataTextField = "Country";
        ddlReCountry.DataValueField = "ID";
        ddlReCountry.DataBind();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //If it has not been an member
        //if (!isLogin())
        //{
            //spanName.Text = txtName.Text;
            //txtMemberEmail.Text = txtEmail.Text;
            //ModalPopupExtender1.Show();
        //}
        //else
        //{
        //    //Call Go on Button
        //    btnGoon_Click(sender, e);
        //}
        btnGoon_Click(sender, e);
    }

    /// <summary>
    /// bind parcel type to ddlParcelType
    /// </summary>
    private void bindParcelType()
    {
        BLLShipment bll = new BLLShipment();
        ddlParcelType.DataSource = bll.getAllParcelType();
        ddlParcelType.DataTextField = "ParcelType";
        ddlParcelType.DataValueField = "ID";
        ddlParcelType.DataBind();
    }

    protected void btnGoon_Click(object sender, EventArgs e)
    {
        BLLShipment bllShipment = new BLLShipment();

        tblCustomer senderCustomer = null;

        if (Session["senderCustomer"] != null)
            senderCustomer = (tblCustomer)Session["senderCustomer"];
        
        //If it is not a member
        if (senderCustomer == null)
        {
            //New Sender Customer
            senderCustomer = new tblCustomer()
            {
                Name = txtName.Text,
                NRIC = txtNRIC.Text,
                Block = txtBlock.Text,
                Street = txtStreet.Text,
                City = txtCity.Text,
                Province = txtProvince.Text,
                Country = Convert.ToInt16(ddlCountry.SelectedValue),
                Contact = txtContact.Text,
                Email = txtEmail.Text,
            };
            bllShipment.NewCustomer(senderCustomer);
        }

        //New Receiver Customer
        tblCustomer receiverCustomer = new tblCustomer()
        {
            Name = txtReName.Text,
            Contact = txtReContact.Text,
            Block = txtReBlock.Text,
            Street = txtReStreet.Text,
            City = txtReCity.Text,
            Province = txtReProvince.Text,
            Country = Convert.ToInt16(ddlReCountry.SelectedValue),
            Email = "",
            IsMember = false
        };
        bllShipment.NewCustomer(receiverCustomer);
        
        //New Package
        tblPackage package = new tblPackage()
        {
            ParcelType = Convert.ToInt16(ddlParcelType.SelectedValue),
            Fragile = Convert.ToBoolean(ddlFragile.SelectedValue),
            Weight = Convert.ToDecimal(txtWeight.Text),
            Sender = senderCustomer.ID,
            Receiver = receiverCustomer.ID,
            Status = 2,
            CreatedDate = DateTime.Now
        };

        bllShipment.NewPackage(package);

        BLLConfirmShipment bll = new BLLConfirmShipment();
        bll.confirmShipment(package.ID, LoginedStaff.BranchID.Value);

       //PrivateFontCollection p = new PrivateFontCollection();
        //p.AddFontFile(Server.MapPath("~/Styles/fre3of9x.ttf"));
        //System.Drawing.Font oFont = new System.Drawing.Font(p.Families[0], 50);
        //bllShipment.GenerateReceipt(package.ID, Server.MapPath("~/Receipt/" + package.Barcode + ".pdf"), oFont);
        Session.Remove("senderCustomer");
        //Response.Redirect("~/Receipt.aspx?barcode=" + package.Barcode);
        string script = "window.open('../ConfirmReceipt.aspx?barcode=" + package.Barcode + "','_blank');window.location.reload();";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "newopen", script, true);
    }

    //protected void btnLogin_Click(object sender, EventArgs e)
    //{
    //    //Do Login Here

    //    if (!isLogin())
    //    {
    //        panelLogin.Style["display"] = "block";
    //        lblLoginFail.Visible = true;
    //        return;
    //    }

    //    bindoginedMemberInfo();
    //    linkNonMember.Visible = false;
    //}

    //private void bindoginedMemberInfo()
    //{
    //    if (isLogin())
    //    {
    //        linkNonMember.Visible = false;
    //        txtName.Text = LoginedMember.Name;
    //        txtEmail.Text = LoginedMember.Email;
    //        txtNRIC.Text = LoginedMember.NRIC;
    //        txtBlock.Text = LoginedMember.Block;
    //        txtStreet.Text = LoginedMember.Street;
    //        txtCity.Text = LoginedMember.City;
    //        txtProvince.Text = LoginedMember.Province;
    //        ddlCountry.SelectedValue = LoginedMember.Country.HasValue ? LoginedMember.Country.Value.ToString() : null;
    //        txtContact.Text = LoginedMember.Contact;
    //    }
    //}

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (txtMemberPassword.Text != txtMemberRePassword.Text)
        {
            lblPasswordError.Visible = true;
            ModalPopupExtender1.Show();
            return;
        }

        try
        {
            BLLRegistration bllRegistration = new BLLRegistration();
            if (bllRegistration.CheckEmailExist(txtMemberEmail.Text))
            {
                ModalPopupExtender1.Hide();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "err", "alert('Email Address has been exist, please choose another email');", true);
                return;
            }

            tblCustomer customer = new tblCustomer()
            {
                Name = txtName.Text,
                NRIC = txtNRIC.Text,
                Block = txtBlock.Text,
                Street = txtStreet.Text,
                City = txtCity.Text,
                Province = txtProvince.Text,
                Country = Convert.ToInt16(ddlCountry.SelectedValue),
                Contact = txtContact.Text,
                Email = txtMemberEmail.Text,
                Password = new Hash().hash(txtMemberPassword.Text),
                IsMember = true
            };

            bllRegistration.register(customer);
            try
            {
                bllRegistration.sendEmail(customer.Email);
            }
            catch { ;}
            //LoginedMember = customer;
            Session["senderCustomer"] = customer;
            btnGoon_Click(sender, e);
        }
        catch
        {

        }
    }
}